package leetCode.solution;

public class plus_one {
	public int[] plusOne(int[] digits) {
		boolean allNine = true;
		for(int i : digits) {
			if(i!=9) {
				allNine = false;
				break;
			}
		}
		if(allNine) {
			int[] result = new int[digits.length+1];
			result[0]=1;
			return result;
		}
		
		int prePlus = 0;
		for(int i =digits.length-1;i>=0;i--) {
			if(i==digits.length-1) {
				if(digits[digits.length-1]==9) {
					digits[digits.length-1]=0;
					prePlus = 1;
				}else {
					digits[digits.length-1]+=1;
					prePlus = 0;
				}
			}else {
				if(digits[i]==9 && prePlus == 1) {
					digits[i]=0;
					prePlus = 1;
				}else {
					digits[i]+=prePlus;
					prePlus = 0;
				}
			}
		}
		return digits;
    }
}
